import Vue from 'vue'

import VueRouter from 'vue-router'

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  routes: [
    {
      //默认跳转路由
      name: 'default',
      path: '/',
      redirect: { name: 'index', }
    },
    {
      //跳转到首页路由
      name: 'index',
      path: '/index',
      component: () => import('@/views/index.vue')
      , meta: { keepAlive: false }
    },
    {
      //跳转到登录路由
      name: 'login',
      path: '/login',
      component: () => import('@/views/login.vue')
    },
    {
      //跳转到注册路由
      name: 'register',
      path: '/register',
      component: () => import('@/views/register.vue')
    },
    {
      //跳转到个人中心路由
      name: 'personal',
      path: '/personal/:id',
      component: () => import('@/views/personal.vue')
    },
    {
      //跳转到编辑个人信息路由
      name: 'profile',
      path: '/profile/:id',
      component: () => import('@/views/profile.vue')
    }
    , {
      //跳转到新闻详情路由
      name: 'articleDetail',
      path: '/articleDetail/:id',
      component: () => import('@/views/articleDetail.vue')
      , meta: { keepAlive: false }
    }
    , {
      //跳转到我的关注路由
      name: 'collection',
      path: '/collection/:id',
      component: () => import('@/views/collection.vue'),
      meta: { keepAlive: false }
    }
    , {
      //跳转到我的收藏路由
      name: 'collection_page',
      path: '/collection_page/:id',
      component: () => import('@/views/collection_page.vue'),
      meta: { keepAlive: false }
    }
    , {
      //跳转到精彩评论路由
      name: 'comment',
      path: '/comment/:id',
      component: () => import('@/views/comment.vue')
    }
    , {
      //跳转到栏目管理
      name: 'management',
      path: '/management',
      component: () => import('@/views/management.vue'),
      meta: { keepAlive: false }
    }
    , {
      //跳转到搜索管理
      name: 'search',
      path: '/search',
      component: () => import('@/views/search.vue'),
      meta: { keepAlive: false }
    }
  ]
});


//导航守卫
router.beforeEach((to, from, next) => {
  // console.log(to);
  let arr = ['/personal']
  if (to.path.indexOf('/personal') !== -1) {
    let token = localStorage.getItem('mytoken')
    if (token) {
      next()
    } else {
      next({ name: 'login' })
    }
  } else {
    next()
  }

})

export default router